package com.aatrox.bhsypro.service.impl;

import com.aatrox.bhsypro.dao.BUserDao;
import com.aatrox.bhsypro.entity.BUser;
import com.aatrox.bhsypro.service.BUserService;
import com.aatrox.bhsypro.util.CookieUtil;
import com.aatrox.bhsypro.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@Service
public class BUserServiceImpl implements BUserService {
    @Autowired
    BUserDao bUserDao;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    //注册
    @Override
    public int zc(BUser bUser) {
        int i=bUserDao.BUserZc(bUser);
        return i;
    }

    //登录
    @Override
    public BUser dl(BUser bUser, HttpServletRequest request , HttpServletResponse response) {
        BUser list =bUserDao.selBUser(bUser);
        String ticket = UUIDUtil.uuid();
        System.out.println("{登录用户:}"+list);
        redisTemplate.opsForValue().set("user:"+ticket+"uid:"+list.getUid(),list);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return list;
    }


    @Override
    public Integer selUserNameIsCf(String uName) {
        Integer num = bUserDao.selUserNameIsCf(uName);
        return num;
    }

    @Override
    public BUser selUserForUid(int uid) {
        BUser user = bUserDao.selUserForUid(uid);
        return user;
    }

    @Override
    public void updateUserVip(int uid) {
        bUserDao.updateUserVip(uid);
    }

    @Override
    public BUser getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+userTicket);
        CookieUtil.setCookie(request,response,"userTicket",userTicket);
        return user;
    }
}
